import Taro from "@tarojs/taro";
import { defineStore } from "pinia";

import { getUserInfo } from "@/api";
import { UserInfoType } from "@/apiTypes";

/**用户信息 */
export const userInfoStore = defineStore("userInfo", {
  state: (): UserInfoType => ({
    whitelistUserData: {}, //更多设置
    selectedWifi: [], //选中的wifi
    punchCards: [], //打卡记录数据

    selectDeptIds: [], //选中的部门ids
    selectUserIds: [], //选中部门的人员ids

    attendanceDeptIds: [], // 考勤部门
    attendanceUserIds: [], // 考勤人员

    whitelistDeptIds: [], //白名单部门
    whitelistUserIds: [], //白名单人员

    selectType: "", //区分是哪个页面传的值，从而给以不同的值赋值
    tabsBottomSwitch: 0,
    isUserPopup: false, //用户信息弹窗
    isAuth: false,
    avatar: "",
    gender: 0,
    inviteCode: "",
    isVip: false,
    mixNickName: "",
    nickName: "",
    openId: "",
    userId: 0,
    lotteryTimes: 0,
    isShare: false,
    totalLottery: 0,
    lotteryCode: [],
    money: 0,
    firstPop: false,
    secondPop: false,
    user: {},
  }),
  /**
   * @author zzw
   * @date 2023-11-22 14:58:11
   * @desc 对state进行更改赋值，支持接口请求
   */
  actions: {
    setAuth() {
      this.$patch({
        isAuth: true,
      });
    },
    /**
     * @author zhangzw
     * @date 2023-12-14 15:27:50
     * @desc 获取用户信息
     */
    async getUserInfo() {
      try {
        const response = await getUserInfo();

        console.log(response.data, "rrr");
        this.$patch({
          user: response.data.user,
        });
        console.log("更新后的user:", this.user);
        Taro.setStorageSync("user", response.data.user);
        console.log(response);

        if (response.success || response.code == 200) {
          this.$patch({
            ...response.data,
          });
        }
      } catch (error) {
        Taro.redirectTo({ url: "/pages/error/index" });
        throw new Error(error.message);
        return;
      }
    },
    initStoreWatcher() {
      this.$subscribe((mutation, state) => {
        const changedKeys = Object.keys(mutation.events || {});
        const shouldUpdate = ["selectDeptIds", "selectUserIds", "selectType"].some(key => changedKeys.includes(key));
        console.log(shouldUpdate, "shouldUpdate");
        console.log(this.selectType, "this.selectType");

        if (shouldUpdate) {
          this.updateAttendanceSelection();
        }
      });
    },
    // setAttendanceDeptIds() {
    //   if (this.selectType === "attendance") {
    //     this.attendanceDeptIds = this.selectDeptIds;
    //     this.attendanceUserIds = this.selectUserIds;
    //   }
    //   console.log(this.selectType, "this.selectType");
    // },
    /**
     * 更新考勤相关选择
     */
    updateAttendanceSelection() {
      if (this.selectType === "attendance") {
        this.$patch({
          attendanceDeptIds: this.selectDeptIds,
          attendanceUserIds: this.selectUserIds,
        });
      } else if (this.selectType === "whitelist") {
        this.$patch({
          whitelistDeptIds: this.selectDeptIds,
          whitelistUserIds: this.selectUserIds,
        });
      }
      console.log(this.selectType, "this.selectType");
    },
    setSelectedWifi(params: any) {
      this.$patch(params);
    },
    setPunchCards(params: any) {
      this.$patch(params);
    },
    // 设置selectDeptIds
    setSelectDeptIds(params: any) {
      this.$patch(params);
    },
    // 设置tabsBottomSwitch
    setTabsBottomSwitch(params: any) {
      this.$patch(params);
    },
    //设置用户信息
    setUser(params) {
      this.$patch(params);
    },

    //设置用户信息弹窗
    setUserPopup() {
      this.$patch({
        isUserPopup: true,
      });
    },
  }, //可以异步
});
